$(document).ready(function(){
	/* Reset value
	clientName      : $('#form_contact_poster_clientName').val('');
	clientEmail     : $('#form_contact_poster_clientEmail').val('');
	clientTelephone : $('#form_contact_poster_clientTelephone').val('');
	clientMessage   : $('#form_contact_poster_clientMessage').val('');
	clientCaptcha   : $('#form_contact_poster_clientCaptcha').val('');
	*/
	 
	// Colect formFields
	var _formFields = [
	  {
	      fieldId      : 'form_contact_poster_clientName',
	      fieldType    : 'text',
	      errorId      : 'clientNameError',
	      errorMessage : 'Tên không hợp lệ (20 kí tự).',
	      required     : true,
	      maxLength    : '20'
	  },
	
	  {
	      fieldId      : 'form_contact_poster_clientTelephone',
	      fieldType    : 'telephone',
	      errorId      : 'clientTelephoneError',
	      errorMessage : 'Điện thoại không hợp lệ.',
	      required     : false
	  },
	
	  {
	      fieldId      : 'form_contact_poster_clientEmail',
	      fieldType    : 'email',
	      errorId      : 'clientEmailError',
	      errorMessage : 'Email không hợp lệ.',
	      required     : true
	  },
	
	  {
	      fieldId      : 'form_contact_poster_clientMessage',
	      fieldType    : 'text',
	      errorId      : 'clientMessageError',
	      errorMessage : 'Nội dung không hợp lệ (500 kí tự).',
	      maxLength    : '500',
	      required     : true
	  }
	];
	
	// onValid function
	var _onValid = function(){
		 // callback function onValid
	    $.post(AJAX_POST_CONTACT_URL, {
	    	clientName      : $('#form_contact_poster_clientName').val(),  
	        clientEmail     : $('#form_contact_poster_clientEmail').val(),
	        clientTelephone : $('#form_contact_poster_clientTelephone').val(),
	        clientMessage   : $('#form_contact_poster_clientMessage').val(),
	        clientCaptcha   : $('#form_contact_poster_clientCaptcha').val(),
	        announcementId  : announcementId
	    },
	    
	    function(error){ // callback function of post contact
	    	// Clean the errors
	    	$('#form_contact_poster .error').each(function(){
	    		$(this).text('');
	    	});

	    	if(error.clientNameError) $('#clientNameError').text('Tên không hợp lệ (20 kí tự).');
	    	if(error.clientEmailError) $('#clientEmailError').text('Email không hợp lệ.');
	    	if(error.clientMessageError) $('#clientMessageError').text('Nội dung không hợp lệ (500 kí tự).');
	    	if(error.clientTelephoneError) $('#clientTelephoneError').text('Điện thoại không hợp lệ.');
	    	if(error.clientCaptchaError) $('#clientCaptchaError').text('Captcha không đúng.');
	    	
	    	// If sendEmailError is defined then check
	    	if (typeof(error.sendEmailError) != "undefined") {
	    		// If error is true, it means there was an error when trying to send email then show error
	    		if (error.sendEmailError) {
	    			$('#sendErrorMessage').addClass('error').text('Có lỗi xảy ra khi gửi tin liên hệ !');
	    			
	    		// There is no error when send mail then close the form and show success message
	    		} else {
	    			$('#expand').remove();
	    			$('#form_contact_poster').slideToggle('fast', function(){
		    			$('#sendErrorMessage').addClass('success').text('Đã được gửi thành công !');
	    			});
	    		}
	    	}  
	    	
	    }, "json");
	};

	// Define onProcess function
	var _onProcess = function(){
		$('#overlay').fadeIn();
	};
	
	// Define onFinish function
	var _onFinish = function(){
		$('#overlay').fadeOut();
	};
	
    // Check form fields
    $('#form_contact_poster').checkForm({
    	formFields : _formFields,
    	onProcess  : _onProcess,
    	onFinish   : _onFinish,
    	onValid    : _onValid,
    	onInvalid  : function(){}
    });
	
    // Handle the expland icon
    $('#expand').bind('click', function(){
    	$('#form_contact_poster').slideToggle();
    });
});